document
		.write("<script type='text/javascript' src='js/jquery-2.0.0.js'></script>");
document
		.write("<script type='text/javascript'  src='layer/layer.js'></script>");
document
		.write("<script type='text/javascript' src='layer/extend/layer.ext.js'></script>");
$(function(){$("#kehu2").click(function() {
						// location.href ='Customer.jsp';
						$(".kuang3")
								.html(
										"<div class='d1'></div><div class='tou1'></div><div class='tou2'></div><div class='tou3'></div>");
						$(".d1").append("<p class='font'>当前位置：客户管理</p>");
						$(".tou3")
								.html(
										"<h style='color:#3786c7;font-weight:blod; font-size:16px'>总共有<h id='number'></h>条数据,每页显示5条数据</h>&nbsp;<input type='button' value='上一页'   onClick='kehushangyiye()' class='jihuabutton'>&nbsp<h id='page'>1</h>&nbsp<input type='button' value='下一页' onClick='kehuxiayiye()'  class='jihuabutton'>");
						$(".tou1")
								.append(
										"<div class='tianjiajihua' id='kehuzengjia'>添加客户</div><input class='sousuo' type='text'><div class='sousuobutton' id='find'>搜索</div>");
						$("#find").click(function() {
							// alert($(".sousuo").val());
							kehufind();

						});
						// $(".sousuo").bind("propertychange input",kehufind());
						// //2222
						// $(".sousuo").bind("focus",function(){
						// kehufind();
						// });
						$(".sousuo").bind("input propertychange", kehufind);

						// $("#xiangmu1").show();
						$("#kehuzengjia")
								.click(
										function() {
											layer
													.open({
														type : 1,
														skin : 'layui-layer-demo',
														area : [ '1000px',
																'200px' ],
														shadeClose : true,
														content : '<form id="kehuzj111" method="post">'
																+ '<center ><table><tr>'

																// +'
																// <th
																// style="width:
																// 20px;
																// height:35px;">客户Id</th>'
																+ '		<th style="width: 20px; height:35px;">客户名称</th>'
																+ '		<th style="width: 20px; height:35px;">客户地址</th>'
																+ '		<th style="width: 20px; height:35px;">创建时间</th></tr>'
																+ '	<tr>'

																// +'
																// <td><input
																// type="text"
																// name="customerId"
																// /></td>'
																+ '      <td><input type="text" name="customerName" id="customerName"/></td>'
																+ '      <td><input type="text" name="customerAddress"  id="customerAddress"/></td>'
																+ '      <td><input type="text" name="createTime" readonly></td></tr></table>'
																+ '<br><input type="button"  onClick="khtj()"  value="确认"  id="anniu" name="确认" $nbsp,$nbsp/> '
																+ '</center>'
																+ '</form>'
													});
										});

						kehu();
					});
});
function getNowFormatDate() {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = date.getFullYear() + seperator1 + month + seperator1
			+ strDate + " " + date.getHours() + seperator2 + date.getMinutes()
			+ seperator2 + date.getSeconds();
	return currentdate;
}
// 客户添加
function khtj() {
	// alert("1");
	// alert($("#customerAddress").val());

	var customerAddress = $("#customerAddress").val();
	var customerName = $("#customerName").val();
	// alert(customerAddress+customerName);
	if (customerAddress == "" || customerName == "") {
		alert("客户名和地址不能为空");
	} else {
		// 返回后台
		$.ajax({
			type : 'post',
			data : $("#kehuzj111").serialize(),// 根表单ID对应
			async : false,
			url : "kehuzengjia123.do", // 后台对应
			dataType : 'json',
			success : function(data) {
				// alert("1");
				if (data.result == "error") {
					alert("客户名已存在");
				} else {
					layer.closeAll();
					kehupage();
					kehuselect();
				}
			}
		});
	}

}
// 客户修改
function kehuxiugai() {
	// 返回后台
	$.ajax({
		type : 'post',
		data : $("#kehuxg").serialize(),// 根表单ID对应
		async : false,
		url : "xiugai.do", // 后台对应
		dataType : 'json',
		success : function(data) {
			// alert("1");
			layer.closeAll();
			kehupage();
			kehuselect();
		}
	});
}
// 客户删除
function kehushanchu() {
	// 返回后台
	$.ajax({
		type : 'post',
		data : $("#kehusc").serialize(),// 根表单ID对应
		async : false,
		url : "customerdel.do", // 后台对应
		dataType : 'json',
		success : function(data) {
			layer.closeAll();
			kehupage();
			kehuselect();
		}
	});
}

function kehu() {
	kehupage();
	$("#page").html("1");
	kehuselect();
}
function kehupage() {
	$.ajax({
		type : 'post',
		data : '',
		async : false,
		url : "kehunumber.do",
		dataType : 'json',
		success : function(data) {
			// alert(data.aaa);
			$("#number").html(data.aaa);
		}
	});
}
function kehushangyiye() {
	var page = $("#page").html();
	if (page == 1) {
		alert("已经是第一页了");
	} else {
		page = page - 1;
		$("#page").html(page);
		kehuselect();
	}
}
function kehuxiayiye() {
	var page = $("#page").html();
	var number = $("#number").html();
	var zong = parseInt(number / 5);
	if (!(number % 5)) {
	} else {
		zong++;
	}
	if (page == zong || number == 0) {
		alert("已经是最后一页了");
	} else {
		page++;
		$("#page").html(page);
		kehuselect();
	}
}
function kehuselect() {
	$
			.ajax({
				type : 'post',
				data : {
					"page" : $("#page").html()
				},
				url : "kehu.do",
				dataType : 'json',
				success : function(data) {
					if (data.length == 0) {
						$(".tou2").html("暂无记录");
					} else {
						$(".tou2").html("");
						$(".tou2")
								.append(
										"<table class='hovertable' style='width:840px'>");
						$(".tou2")
								.append(
										"<tr>"
												+ "      <th scope='col' style='width: 250px; '><center>客户Id</center></th>"
												+ "		<th scope='col' style='width: 250px; '><center>客户名称</center></th>"
												+ "		<th scope='col' style='width: 100px; '><center>客户地址</center></th>"
												+ "		<th scope='col' style='width: 250px; '><center>创建时间</center></th>"
												+ "		<th scope='col' style='width: 100px; '><center>修改</center></th>"
												+ "		<th scope='col' style='width: 100px; '><center>删除</center></th>"
												+ "   </tr>");

						for ( var i = 0; i < data.length; i++) {
							$(".tou2")
									.append(
											"<tr>"
													+ "<td class='row' id='type'><center>"
													+ data[i].customerId
													+ "</center></td>"
													+ "        <td class='row' id='name'><center>"
													+ data[i].customerName
													+ "</center></td>"
													+ "        <td class='row' id='status'><center>"
													+ data[i].customerAddress
													+ "</center></td>"
													+ "        <td class='row' id='time'><center>"
													+ data[i].createTime
													+ "</center></td>"
													+ "       <td class='row' id='"
													+ i
													+ "' name='xiugai'><center><img src='images/mod.gif'  ></center></td>"
													+ "        <td class='row' id='"
													+ i
													+ "' name='shanchu'><center><img src='images/del.gif'></center></td>"
													+ "    </tr>");
						}

						$(".tou2").append("</table>");
						$(".tou2").append("<div style='height:15px'></div>");
						// $("td[name='shanchu']").click(
						// function() {
						// var getid=data[this.id].customerId;
						// alert(getid);
						// //询问框
						// layer.confirm('你是否要删除？', {
						// btn: ['确定','取消'] //按钮
						// }, function(){
						// $.ajax({
						// type : 'post',
						// data : {
						// "customer" : getid
						// },
						// url : "customerdel.do",
						// dataType : 'json',
						// success :function(){
						// alert("删除成功");
						// layer.closeAll();
						// kehuselect();
						// }
						// }
						// );
						// }, function(){
						// layer.closeAll();
						// });
						// });
						$("td[name='shanchu']")
								.click(
										function() {
											var id = this.id;
											layer
													.open({
														type : 1,
														skin : 'layui-layer-demo',
														area : [ '1000px',
																'200px' ],
														shadeClose : true,
														content : '<form method="post" id="kehusc">'
																+ '<center id="shanchuId"><table id="tb1"><tr>'
																+ '		<th style="width: 20px; height:25px;">客户ID1</th>'
																+ '		<th style="width: 20px; height:25px;">客户名称1</th>'
																+ '		<th style="width: 20px; height:25px;">客户地址1</th>'
																+ '		<th style="width: 20px; height:25px;">创建时间1</th></tr>'
																+ '	<tr>'
																+ '      <td><input type="text" name="CustomerId" value="'
																+ data[id].customerId
																+ '"/ readonly></td>'
																+ '      <td><input type="text" name="CustomerName" value="'
																+ data[id].customerName
																+ '"></td>'
																+ '      <td><input type="text" name="CustomerAddress" value="'
																+ data[id].customerAddress
																+ '"/></td>'
																+ '      <td><input type="text" name="createTime" value="'
																+ data[id].createTime
																+ '"/ readonly></td></tr></table>'
																+ '<br><input type="button" value="确认删除" onClick="kehushanchu()" /></center>'
																+ '</form>'

													});

										});

						$("td[name='xiugai']")
								.click(
										function() {
											var id = this.id;
											layer
													.open({
														type : 1,
														skin : 'layui-layer-demo',
														area : [ '1000px',
																'200px' ],
														shadeClose : true,
														content : '<form method="post" id="kehuxg">'
																+ '<center id="xiugaiId"><table id="tb1"><tr>'
																+ '		<th style="width: 20px; height:25px;">客户ID1</th>'
																+ '		<th style="width: 20px; height:25px;">客户名称1</th>'
																+ '		<th style="width: 20px; height:25px;">客户地址1</th>'
																+ '		<th style="width: 20px; height:25px;">创建时间1</th></tr>'
																+ '	<tr>'
																+ '      <td><input type="text" name="CustomerId" value="'
																+ data[id].customerId
																+ '"/ readonly></td>'
																+ '      <td><input type="text" name="CustomerName" value="'
																+ data[id].customerName
																+ '"></td>'
																+ '      <td><input type="text" name="CustomerAddress" value="'
																+ data[id].customerAddress
																+ '"/></td>'
																+ '      <td><input type="text" name="createTime" value="'
																+ data[id].createTime
																+ '"/ readonly></td></tr></table>'
																+ '<br><input type="button" value="确认修改" onClick="kehuxiugai()" /></center>'
																+ '</form>'

													});

										});

					}
				},
				error : function(data) {
					alert("错误");
				}

			});

	// $("#version").css("background","#27a2e6");
	event.stopPropagation();

}

function kehufind() {
	$
			.ajax({
				type : 'post',
				data : {
					"customerName" : $(".sousuo").val(),
					"page" : $("#page").html()
				},
				url : "kehufind.do",
				dataType : 'json',
				success : function(data) {
					if (data.length == 0) {
						$(".tou2").html("暂无记录");
					} else {
						$(".tou2").html("");
						$(".tou2")
								.append(
										"<table class='hovertable' style='width:840px'>");
						$(".tou2")
								.append(
										"<tr>"
												+ "      <th scope='col' style='width: 250px; '><center>客户Id</center></th>"
												+ "		<th scope='col' style='width: 250px; '><center>客户名称</center></th>"
												+ "		<th scope='col' style='width: 100px; '><center>客户地址</center></th>"
												+ "		<th scope='col' style='width: 250px; '><center>创建时间</center></th>"
												+ "		<th scope='col' style='width: 100px; '><center>修改</center></th>"
												+ "		<th scope='col' style='width: 100px; '><center>删除</center></th>"
												+ "   </tr>");

						for ( var i = 0; i < data.length; i++) {
							$(".tou2")
									.append(
											"<tr>"
													+ "<td class='row' id='type'><center>"
													+ data[i].customerId
													+ "</center></td>"
													+ "<td class='row' id='name'><center>"
													+ data[i].customerName
													+ "</center></td>"
													+ "        <td class='row' id='status'><center>"
													+ data[i].customerAddress
													+ "</center></td>"
													+ "        <td class='row' id='time'><center>"
													+ data[i].createTime
													+ "</center></td>"
													+ "       <td class='row' id='"+ i+ "' name='xiugai'><center><img src='images/mod.gif'  ></center></td>"
													+ "       <td class='row' id='"+ i+ "' name='shanchu'><center><img src='images/del.gif'></center></td>"
													+ "    </tr>");
						}

						$(".tou2").append("</table>");
						$(".tou2").append("<div style='height:15px'></div>");

						$("td[name='shanchu']").click(function() {
											var id = this.id;
											layer
													.open({
														type : 1,
														skin : 'layui-layer-demo',
														area : [ '1000px',
																'200px' ],
														shadeClose : true,
														content : '<form method="post" id="kehusc">'
																+ '<center id="shanchuId"><table id="tb1"><tr>'
																+ '		<th style="width: 20px; height:25px;">客户ID1</th>'
																+ '		<th style="width: 20px; height:25px;">客户名称1</th>'
																+ '		<th style="width: 20px; height:25px;">客户地址1</th>'
																+ '		<th style="width: 20px; height:25px;">创建时间1</th></tr>'
																+ '	<tr>'
																+ '      <td><input type="text" name="CustomerId" value="'
																+ data[id].customerId
																+ '"/ readonly></td>'
																+ '      <td><input type="text" name="CustomerName" value="'
																+ data[id].customerName
																+ '"></td>'
																+ '      <td><input type="text" name="CustomerAddress" value="'
																+ data[id].customerAddress
																+ '"/></td>'
																+ '      <td><input type="text" name="createTime" value="'
																+ data[id].createTime
																+ '"/ readonly></td></tr></table>'
																+ '<br><input type="button" value="确认删除" onClick="kehushanchu()" /></center>'
																+ '</form>'

													});

										});

						$("td[name='xiugai']")
								.click(
										function() {
											var id = this.id;
											layer
													.open({
														type : 1,
														skin : 'layui-layer-demo',
														area : [ '1000px',
																'200px' ],
														shadeClose : true,
														content : '<form method="post" id="kehuxg">'
																+ '<center id="xiugaiId"><table id="tb1"><tr>'
																+ '		<th style="width: 20px; height:25px;">客户ID1</th>'
																+ '		<th style="width: 20px; height:25px;">客户名称1</th>'
																+ '		<th style="width: 20px; height:25px;">客户地址1</th>'
																+ '		<th style="width: 20px; height:25px;">创建时间1</th></tr>'
																+ '	<tr>'
																+ '      <td><input type="text" name="CustomerId" value="'
																+ data[id].customerId
																+ '"/ readonly></td>'
																+ '      <td><input type="text" name="CustomerName" value="'
																+ data[id].customerName
																+ '"></td>'
																+ '      <td><input type="text" name="CustomerAddress" value="'
																+ data[id].customerAddress
																+ '"/></td>'
																+ '      <td><input type="text" name="createTime" value="'
																+ data[id].createTime
																+ '"/ readonly></td></tr></table>'
																+ '<br><input type="button" value="确认修改" onClick="kehuxiugai()" /></center>'
																+ '</form>'

													});

										});

					}
				},
				error : function(data) {
					// alert("错误");
				}

			});

	event.stopPropagation();

}